package maliklund87.spiritrpg;

import maliklund87.spiritrpg.dao.ImageLoader;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

public class MainMenu implements LogicHandler {
	private LogicManager logicManager;
	private Image backgroundImg, newGameImg;
	private float newGameImgX, newGameImgY;
	
	public MainMenu(LogicManager logicManager){
		this.logicManager = logicManager;
	}

	@Override
	public void render(GameContainer gameContainer, Graphics graphics)
			throws SlickException {
		backgroundImg.draw(0, 0);
		newGameImg.draw(newGameImgX, newGameImgY);
	}

	@Override
	public void init(GameContainer gameContainer) throws SlickException {
		backgroundImg = ImageLoader.loadImage("data/mainmenu_background.png");
		newGameImg = ImageLoader.loadImage("data/mainmenu_newgame.png");
		newGameImgX = 800; newGameImgY = 500;
	}

	@Override
	public void update(GameContainer gameContainer, float delta)
			throws SlickException {
		Input input = gameContainer.getInput();
		if (input.isKeyPressed(Input.KEY_ESCAPE)){
			logicManager.setLogicHandler(null);
		}
		if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON) 
				&& collides(input.getMouseX(), input.getMouseY(), newGameImg)){
		    logicManager.setLogicHandler(new BattleScene(logicManager));			
		}

	}

	private boolean collides(int mouseX, int mouseY, Image img) {
		boolean result = false;
		if (mouseX > newGameImgX && mouseX < (newGameImgX + newGameImg.getWidth()) &&
			mouseY > newGameImgY && mouseY < (newGameImgY + newGameImg.getHeight())){
			result = true;
		}
		return result;
	}

	@Override
	public void exit() {
		ImageLoader.unloadImage(backgroundImg);
		backgroundImg = null;
	}

}
